<template>
  <div class="login1">
    <div>
      <el-form
          v-loading="loading"
          element-loading-text="loading..."
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          :rules="rules"
          ref="loginForm"
          :model="loginForm"
          class="loginContainer1"
      >
        <h3 class="loginTitle">注册</h3>

        <el-form-item prop="username">
          <el-input
              type="text"
              auto-complete="false"
              v-model="loginForm.username"
              placeholder="请输入用户名"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              type="password"
              auto-complete="false"
              v-model="loginForm.password"
              placeholder="请输入密码"
          ></el-input>
        </el-form-item>

        <el-form-item prop="cpassword">
          <el-input
              type="cpassword"
              auto-complete="false"
              v-model="loginForm.cpassword"
              placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input
              type="email"
              auto-complete="false"
              v-model="loginForm.email"
              placeholder="请输入QQ邮箱"
          ></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-input
              size="normal"
              type="text"
              auto-complete="false"
              v-model="loginForm.code"
              placeholder="点击图片更换验证码"
              style="width: 250px; margin-right: 5px"
          ></el-input>
          <img :src="captchaUrl" @click="updateCaptcha" />
        </el-form-item>

        <el-button type="primary" style=" width: 100%" @click="submitLogin" @keyup.enter="submitLogin"
        >注册</el-button
        >

      </el-form>
    </div>
    <vue-particles
        color="#fff"
        :particleOpacity="0.7"
        :particlesNumber="60"
        shapeType="circle"
        :particleSize="4"
        linesColor="#fff"
        :linesWidth="1"
        :lineLinked="true"
        :lineOpacity="0.4"
        :linesDistance="150"
        :moveSpeed="2"
        :hoverEffect="true"
        hoverMode="grab"
        :clickEffect="true"
        clickMode="push"
        class="lizi"
        style="height: 100%"
    ></vue-particles>
  </div>
</template>


<script>


import {Message} from "element-ui";

export default {
  name: "Login",
  data() {
    return {
      captchaUrl: "/captcha?time=" + new Date(),
      loginForm: {
        username: "",
        password: "",
        email: "",
        agreed: false,
        cpassword:"",
        code: "",
      },
      loading: false,
      checked: true,
      rules: {
        agreed: [{
          validator: (rule, value, callback) => {
            // console.log('value:' + value)
            if (value !== true) {
              callback(new Error('请确认同意注册协议'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }],
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        email: [{ required: true, message: "请输入QQ邮箱", trigger: "blur" }],
        cpassword: [{
          required: true,
          message: '确认密码',
          trigger: 'blur'
        }, {
          validator: (rule, value, callback) => {
            if (value === '') {
              callback(new Error('请再次输入密码'))
            } else if (value !== this.loginForm.password) {
              callback(new Error('两次输入密码不一致'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }]
      },
    };
  },
  methods: {
    updateCaptcha() {
      this.captchaUrl = "/captcha?time=" + new Date();
    },
    submitLogin() {
      this.loading = true;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.postRequest('/register',this.loginForm).then(resp=>{
            if(resp){
              this.loading = false;
              Message.success("注册成功！")
              this.$router.replace('/Login');
            }
          })
        } else {
          this.$message.error("请输入全部信息！");
          return false;
        }
      });
    },
  },
};
</script>


<style>

</style>
